import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Wlecome from '../components/Wlecome.vue'
// import Users from '../components/user/User.vue'
// // 权限组件
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
// // 商品
// import Categery from '../components/goods/Categery.vue'
// import params from '../components/goods/Params.vue'
// import List from '../components/goods/List.vue'
// import Add from '../components/goods/Add.vue'
// // 订单列表
// import Order from '../components/order/Order.vue'
// // 数据报表
// import Report from '../components/report/Report.vue'
// 路由懒加载
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue')
const Wlecome = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Wlecome.vue')

const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/User.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

const Categery = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Categery.vue')
const params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')

const List = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/Add.vue')

const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Report.vue')
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    // 重新定位
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      // 重定位
      redirect: '/welcome',
      // 子路由
      children: [
        {
          path: '/welcome',
          component: Wlecome
        },
        // 子级用户路由
        {
          path: '/users',
          component: Users
        },
        // 子级权限路由
        {
          path: '/rights',
          component: Rights
        },
        // 自己角色陆游
        {
          path: '/roles',
          component: Roles
        },
        // 商品分类陆游
        {
          path: '/categories',
          component: Categery
        },
        // 商品参数
        {
          path: '/params',
          component: params
        },
        // 商品列表
        {
          path: '/goods',
          component: List
        },
        // 添加商品
        {
          path: '/goods/add',
          component: Add
        },
        // 订单列表
        {
          path: '/orders',
          component: Order
        },
        // 数据报表
        {
          path: '/reports',
          component: Report
        }
      ]
    }
  ]
})
// 拆分挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token直接进入登录页面
  if (!tokenStr) return next('/login')
  // 成功后放行
  next()
})
export default router
